<?php

namespace App\Http\Controllers\home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;

class WishlistController extends BaseController
{
    //心愿单列表
    public function index()
    {
        $this->go_login();
        $limit = 5;//每页显示最多5条数据
        $user_name = Cookie::get('u_name');
        // echo $user_name;die;
        $user = DB::table('user') -> select("user_id") -> where("user_name",$user_name) -> first();
        $user_id = $user->user_id;

        // DB::connection()->enableQueryLog();
        $wishlist = DB::table("wish as w") 
                    -> leftJoin("good as g","g.good_id","w.good_id")
                    -> leftJoin("goodcolor as gc","gc.goodcolor_id","w.goodcolor_id")
                    -> leftJoin("goodsize as gz","gz.goodsize_id","w.goodsize_id")
                    -> where("user_id",$user_id)
                    -> get();
        // dump(DB::getQueryLOg());die;
        
        // dd($wishlist);die;
        return view('home/wishlist',compact("wishlist"));
    }

    //添加心愿单
    public function addwish($good_id)
    {
        $user_name = Cookie::get('u_name');
        // echo $user_name;die;
        $user = DB::table('user') -> select("user_id") -> where("user_name",$user_name) -> first();
        $user_id = $user->user_id;

        $good = DB::table('good') -> whereRaw("good_id = $good_id AND user_id = $user_id") -> first();

        $data = array(
            'user_id' => $user_id,
            'good_id' => $good_id,
            'goodcolor_id' => $good->goodcolor_id,
            'goodsize_id' => $good->goodsize_id,
        );

        $res = DB::table('wish') -> whereRaw("good_id = $good_id AND user_id = $user_id") -> insert($data);
        // if($res){
        //     // return redirect("/")
        // }else{
        //     return 
        // }
        
    }

    //取消心愿单
    public function delete($wish_id)
    {
        $res = DB::table('wish') -> whereRaw("wish_id = $wish_id") -> delete();
        if($res){
            return redirect("/wishlist");
        }else{
            return redirect("/wishlist");
        }
    }
    
}
